package com.yc;

import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Test8_url_httpurlconnection {
    private static Logger log = Logger.getLogger(Test8_url_httpurlconnection.class.getName());
    public static void main(String[] args) throws IOException {
        //URL：统一资源定位符
        URL url = new URL("http://ww.baidu.com");
        URLConnection con = url.openConnection();

        String  contenttype=con.getContentType();
        long conlength =con.getContentLength();
        log.info(contenttype+"/t"+conlength);

        //响应头没有，响应头的内容已经被解析变成了con中的属性值
        InputStream iis= con.getInputStream();

        byte[] bt = new byte[10*1024];
        ByteArrayOutputStream baos =new ByteArrayOutputStream();
        int length= -1;
        while( (length =iis.read(bt,0,bt.length))!=-1){
            baos.write(bt,0,length);
        }
        baos.flush();
        byte[] bb=baos.toByteArray();
        String str =new String(bb);
        System.out.println(str);


        ////////////////////////////////////////////////////////////
        //URL +HTTPURLConnection
//        URL url = new URL("http://www.hyycinfo.com/");
//        HttpURLConnection con = (HttpURLConnection) url.openConnection();
//
//        con.setRequestProperty("HOST","www.hyycinfo.com");
//        con.setRequestMethod("GET");
////        con.setRequestProperty();
//        con.setDoInput(true);
//        con.setDoOutput(true);
//
//        String contentType =con.getContentType();
//        long conlength =con.getContentLength();
//        log.info(contentType+"\t"+conlength);
//
//
//        InputStream iis= con.getInputStream();
//
//        byte[] bt = new byte[10*1024];
//        ByteArrayOutputStream baos =new ByteArrayOutputStream();
//        int length= -1;
//        while( (length =iis.read(bt,0,bt.length))!=-1){
//            baos.write(bt,0,length);
//        }
//        baos.flush();
//
//        byte[] bb=baos.toByteArray();
//        String str =new String(bb);
//        System.out.println(str);//响应头   响应实体
    }
}
